﻿using System;
using System.Xml.Linq;
namespace Skyper.Model {
	public sealed class MessageModel : BaseModel {
		#region Fields
		public const String XName = "Message";
		private String text;
		private DateTime time;
		private bool owner;
		#endregion
		#region Constructor
		/// <summary>
		/// Initializes a new instance of the <see cref="MessageModel"/> class.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="time">The time.</param>
		public MessageModel(String text, DateTime time) : this(text,time,true) {

		}
		/// <summary>
		/// Initializes a new instance of the <see cref="MessageModel"/> class.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="time">The time.</param>
		/// <param name="owner">if set to <c>true</c> [owner].</param>
		public MessageModel(String text, DateTime time, bool owner) {
			this.text = text;
			this.time = time;
			this.owner = owner;
		}
		#endregion
		#region Public
		/// <summary>
		/// Returns a <see cref="System.String"/> that represents this instance.
		/// </summary>
		/// <returns>
		/// A <see cref="System.String"/> that represents this instance.
		/// </returns>
		public sealed override string ToString() {
			if (owner) {
				return String.Format("[{0}] -> {1}",this.time.ToShortTimeString(),this.text);
			} else {
				return String.Format("[{0}] <- {1}", this.time.ToShortTimeString(), this.text);
			}
		}
		/// <summary>
		/// Suspends this model to XElement.
		/// </summary>
		/// <returns>
		/// XElement or Null if suspending is not possible.
		/// </returns>
		public sealed override XElement Suspend() {
			XElement node = new XElement(XName);

			node.SetAttributeValue("Owner", this.owner);
			node.SetAttributeValue("Time", this.time);
			node.SetAttributeValue("Text", this.text);

			return node;
		}
		/// <summary>
		/// Resumes this element, with specified node.
		/// </summary>
		/// <param name="node">The node.</param>
		/// <returns>
		/// True, if resuming proccess was success.
		/// </returns>
		public sealed override bool Resume(XElement node) {
			if (node == null || node.Name != XName) {
				return false;
			}

			if (!node.TryGetAttributeValue("Owner", out this.owner)) {
				return false;
			}
			if (!node.TryGetAttributeValue("Date", out this.time)) {
				return false;
			}

			this.text = node.GetAttributeValue("Text");

			return true;
		}
		#endregion
		#region Property
		/// <summary>
		/// Gets or sets the text of the message.
		/// </summary>
		/// <value>The text.</value>
		public String Text {
			get {
				return this.text;
			}
			set {
				this.text = value;
			}
		}
		/// <summary>
		/// Gets the time of message.
		/// </summary>
		/// <value>The time.</value>
		public DateTime Time {
			get {
				return this.time;
			}
		}
		/// <summary>
		/// Gets a value indicating whether this <see cref="MessageModel"/> is sent from me.
		/// </summary>
		/// <value><c>true</c> if i am owner; otherwise, <c>false</c>.</value>
		public Boolean Owner {
			get {
				return this.owner;
			}
		}
		#endregion
	}
}
